<?php
// +----------------------------------------------------------------------
// | IPHP
// +----------------------------------------------------------------------
// | This is an open source and simple PHP FrameWork.
// +----------------------------------------------------------------------
// | $Id$
// +----------------------------------------------------------------------

// Session支持

namespace IPHP\Core;

class Session {

    // cookie过气时间
    public $exptime = 3600;
    // cookie作用域
    public $domain;

    /**
     * 构造函数
     */
    public function __construct() {
        $this->domain = $_SERVER['SERVER_NAME'];

        if(!IS_SAE) {
        // 使用Session使用cookie
            ini_set('session.use_cookie', 1);
            ini_set('session.cookie_path', '/');
            ini_set('session.cookie_domain', $this->domain);
            ini_set('session.gc_maxlifetime', $this->exptime);   
        }
        //启动session
        session_start();
    }

    /**
     * 获取session值，如果没有传入name,则返回全部
     * @param  string $name [description]
     * @return [type]       [description]
     */
    static public function get($name ="") {
        if($name) {
            if(isset($_SESSION[$name])) {
                return $_SESSION[$name];
            } else {
                return null;
            }
        } else {
            return $_SESSION;
        }
    }

    /**
     * 设置session值
     * @param [type] $name [description]
     * @param [type] $val  如果值为null，则移除当有键值
     */
    static public function set($name,$val) {
        if(!$name) return false;
        if($val === null) {
            unset($_SESSION[$name]);
        } else {
            unset($_SESSION[$name]);
            $_SESSION[$name] = $val;
        }
        return true;
    }


    /**
     * 将Session 清空
     * @return [type] [description]
     */
    public function clean() {
        $_SESSION = array();
    }
   
    /**
     * 销毁Session
     * @return [type] [description]
     */
    public function destroy() {
        unset($_SESSION);
        session_destroy();
    }
}